
//获取应用实例
var app = getApp()
Page({
  data: {
    cartList: [],
    selectHide: false,
    inputValue: '',
    getSearch: [],
    // resultHidden: true,
    searchHidden: false,
    iconHidden: true,
    currentTab: 1,
    ishiddenproduct: false,
    isOnload: true,
  },
  bindInput: function (e) {//输入框输入值时将其设置为当前页面全局变量
    this.setData({
      inputValue: e.detail.value
    })
  },

  onShareAppMessage: function (res) {
    return {
      title: '美甲美睫半永久化妆造型',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  setSearchStorage: function () {
    var that = this;
    this.setData({
      types2Hidden: true,
      ishiddenproduct: false
    });
    let data;
    let localStorageValue = [];
    if (this.data.inputValue != '') {
      onSearchGoods2(that, "", this.data.inputValue)
      //调用API从本地缓存中获取数据
      var searchData = wx.getStorageSync('searchData') || []
      var config = false;
      for (var x in searchData) {
        if (searchData[x] === this.data.inputValue) {
          config = true;
          this.setData({
            resultHidden: false,
            searchHidden: true,
            iconHidden: false
          })
        }
      }
      if (!config) {
        searchData.unshift(this.data.inputValue)
        if (searchData.length > 10) {
          searchData.pop();
          wx.setStorageSync('searchData', searchData)
        } else {
          wx.setStorageSync('searchData', searchData)
        }

        this.setData({
          resultHidden: false,
          searchHidden: true,
          iconHidden: false
        })
      }

    }
  },
  searchGoods2: function (e) {
    onSearchGoods2(this, e.target.dataset.ids, "");
    this.setData({
      types2Hidden: true
    })
  },
  clearSearchStorage: function () {//清除本地缓存的搜索信息
  console.log("dssdfsd");
    this.setData({
      getSearch: [],
    })
    wx.setStorage({
      key: 'searchData',
      data: [],
    })
  },
  onLoad: function (options) {
    app.showShareMenus();
    this.setData({
      isOnload: true,      
    });
    var that = this;    
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          imageUrl: app.globalData.imageUrl
        })
      }
    });
  },
  onShow: function () {//获取本地缓存的搜索信息
   // getShipping(this)//获取配送费以及超市名称，并储存在本地
    var getSearch = wx.getStorageSync('searchData');
    this.setData({
      getSearch: getSearch,
      inputValue: '',
    })
    if (wx.getStorageSync('cartLists')) {
      this.setData({
        cartList: wx.getStorageSync('cartLists')
      })
    } else {
      this.setData({
        cartList: []
      })
    }
  },
  onHide: function () {
  },
  addToSearch: function (e) {//点击历史搜索记录时将记录添加到搜索框中
    this.setData({
      inputValue: e.target.dataset.name
    });
    this.setSearchStorage();
  },
  inputFocus: function () {//当输入框再次聚焦时让搜索内容隐藏，出现历史搜索记录
    var getSearch = wx.getStorageSync('searchData');
    this.setData({
      getSearch: getSearch,
      resultHidden: true,
      searchHidden: false,
      iconHidden: true,
      ishiddenproduct: true
    })
  },
  // hiddenHistory:function(){
  //   this.setData({
  //     searchHidden: true,
  //   })
  // },
  clearInputSearch: function () {//点击清楚按钮清楚当前搜索框的值
    this.setData({
      inputValue: ''
    })
  },
  sendCartProduct: function () {
    if (this.data.cartList != "") {
      wx.setStorageSync('cartLists', this.data.cartList)
    }
  },
  sendCartProduct: function () {//将购物车里的商品储存在本地
    if (this.data.cartList != "") {
      wx.setStorageSync('cartLists', this.data.cartList)
    }
  },
  productDetail: function (e) {//点击单件商品跳转到详情页面
    // console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../directDetail/directDetail?id=' + e.currentTarget.id,
    })
  },
  sendMsgToCart: function (e) {//点击加入购物车时将数据添加至购物车
    var that = this;
    var flag = false;
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      if (!config) {
        getCartInfo(that, e)//加入购物车
      }
    } else {
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('cartLists', this.data.cartList)
    // console.log()
  }
  ,
  toProductsDetail:function(e){
    wx.navigateTo({
      url: '../../pages/directDetail/directDetail?id='+e.target.id,
    })
  }
})
function getCartInfo(that, e) {//加入购物车，将商品信息储存在
  //console.log(that.data.cartList)
  var CartName = e.target.dataset.name;
  var CartPrice = e.target.dataset.price;
  var CartID = e.target.dataset.id;
  var CartSuper = e.target.dataset.name;
  var Cartquantity = 1;
  var totalPrices = "";
  var Cartimg = e.target.dataset.img
  var Cartrepertory = e.target.dataset.repertory
  var carts = { productName: CartName, unitPrice: CartPrice, productId: CartID, quantity: Cartquantity, totalPrices: totalPrices, repertory: Cartrepertory, img: Cartimg }
  that.data.cartList.push(carts);
}








function onSearchGoods2(that, id, name) {
  wx.request({
    url: app.globalData.allUrl + "/goods/condition",
    data: {
      goods_name: that.data.inputValue,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res.data)
      if (res.data) {
        that.setData({
          DirectProduct: res.data,
        })

      }
    }
  })
}

